﻿#pragma execution_character_set("utf-8")

#include "TagsDlg.h"

#include <QAction>
#include <QBoxLayout>
#include <QCheckBox>
#include <QJsonArray>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
#include <QStringList>

#include "FlowLayout.h"
#include "HttpPost.h"
#include "MainWindow.h"
#include "QScrollArea"

TagsDlg::TagsDlg(QWidget* parent)
    : QDialog(parent)
{
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint);
    QVBoxLayout* layout = new QVBoxLayout(this);

    QScrollArea* area = new QScrollArea(this);
    area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    area->setWidgetResizable(true);

    FlowLayout* flowLayout = new FlowLayout();
    area->setLayout(flowLayout);

    auto btn1 = new QPushButton("确定");
    connect(btn1, &QPushButton::clicked, this, &TagsDlg::slotOK);
    flowLayout->addWidget(btn1);

    auto btn2 = new QPushButton("取消");
    connect(btn2, &QPushButton::clicked, [this] { this->reject(); });
    flowLayout->addWidget(btn2);

    auto btn3 = new QPushButton("重置");
    connect(btn3, &QPushButton::clicked, this, &TagsDlg::slotReset);
    flowLayout->addWidget(btn3);
    flowLayout->addEmptyRow(20);

    m_dataMgr = DataMgr::getInstance();

    // atTag
    for (const auto& r : m_dataMgr->m_vecLuoGuRootTag)
    {
        auto btns = createOneRoot(r);
        for (auto b : btns)
        {
            flowLayout->addWidget(b);
        }
    }

    flowLayout->addEmptyRow(40);
    // cfTag
    auto title = new QLabel();
    title->setText(
        "<span style='font-size: 12pt;color:blue'>Codeforces "
        "官方算法分类</span>");
    flowLayout->addWidget(title);
    flowLayout->addEmptyRow(2);

    const std::set<QString>& setCFHaved =
        DataMgr::getInstance()->m_setCFSearchTag;
    const std::vector<AlgoTag>& vecTags = m_dataMgr->m_vecCFAllTags;
    for (const auto& [_id, _text] : vecTags)
    {
        auto btn = new QPushButton(_text);
        btn->setStyleSheet(
            "QPushButton:checked {"
            "   background-color: #4CAF50;"  // 选中时的背景色
            "   color: white;"               // 选中时的文字颜色
            "border: 1px solid #000080;"
            "}");

        btn->setCheckable(true);
        btn->setProperty("id", _id);
        if (setCFHaved.find(_id) != setCFHaved.end())
        {
            btn->setChecked(true);
        }
        btn->setSizePolicy(
            QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
        btn->setMinimumWidth(
            QFontMetrics(btn->font()).boundingRect(_text).width() + 20);
        flowLayout->addWidget(btn);
        m_vecCFTag.push_back(btn);
    }

    flowLayout->addEmptyRow(20);
    btn1 = new QPushButton("确定");
    connect(btn1, &QPushButton::clicked, this, &TagsDlg::slotOK);
    flowLayout->addWidget(btn1);

    btn2 = new QPushButton("取消");
    connect(btn2, &QPushButton::clicked, [this] { this->reject(); });
    flowLayout->addWidget(btn2);

    btn3 = new QPushButton("重置");
    connect(btn3, &QPushButton::clicked, this, &TagsDlg::slotReset);
    flowLayout->addWidget(btn3);

    QWidget* wgt = new QWidget();
    // wgt->setStyleSheet("background-color: yellow;");
    wgt->setLayout(flowLayout);

    area->setWidget(wgt);

    layout->addWidget(area);
    showMaximized();
}

void TagsDlg::slotOK()
{
    // luogu
    std::set<QString> setLuoGuTag;
    for (auto b : m_vecLuoGuTag)
    {
        if (b->isChecked())
        {
            setLuoGuTag.insert(b->property("id").toString());
        }
    }
    DataMgr::getInstance()->m_setLuoGuSearchTag = setLuoGuTag;

    // cf
    std::set<QString> setCFTag;
    for (auto b : m_vecCFTag)
    {
        if (b->isChecked())
        {
            setCFTag.insert(b->property("id").toString());
        }
    }
    DataMgr::getInstance()->m_setCFSearchTag = setCFTag;

    accept();
}

void TagsDlg::slotReset()
{
    for (auto b : m_vecCFTag)
    {
        b->setChecked(false);
    }

    for (auto b : m_vecLuoGuTag)
    {
        b->setChecked(false);
    }
}

std::vector<QWidget*> TagsDlg::createOneRoot(const LuoGuTagRoot& root)
{
    std::vector<QWidget*> vecRet;
    const std::set<QString> setLuoGuHaved =
        DataMgr::getInstance()->m_setLuoGuSearchTag;
    auto title = new QLabel();
    title->setText("<span style='font-size: 12pt;color:blue'>" + root.strName +
                   "</span>");
    vecRet.push_back(title);
    vecRet.push_back(FlowLayout::emptyRow(2));

    for (const auto& [_id, _text] : root.vecTag)
    {
        auto btn = new QPushButton(_text);
        btn->setCheckable(true);
        btn->setStyleSheet(
            "QPushButton:checked {"
            "background-color: blue;"
            "color: white;"
            "border: 1px solid #000080;"
            "}");

        btn->setProperty("id", QString::number(_id));

        if (setLuoGuHaved.find(QString::number(_id)) != setLuoGuHaved.end())
        {
            btn->setChecked(true);
        }

        btn->setSizePolicy(
            QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed));
        btn->setMaximumWidth(
            QFontMetrics(btn->font()).boundingRect(_text).width() + 20);
        m_vecLuoGuTag.push_back(btn);
        vecRet.push_back(btn);
    }
    vecRet.push_back(FlowLayout::emptyRow(20));

    return vecRet;
}
